/* eslint-disable no-unused-vars */
<template>
  <div class="login_container">
    <!-- 登录盒子 -->
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单  :rules绑定的是验证规则 ref是重置相关的-->
      <el-form
        label-width="0px"
        class="form_box"
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
      >
        <!-- 用户名 prop绑定的也是验证规则 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-users" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 prop绑定的也是验证规则-->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="Login">登录</el-button>
          <el-button type="info" @click="loginFormReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // 表单的默认数据
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单的验证规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginFormReset() {
      // console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    Login() {
      this.$refs.loginFormRef.validate(async valid => {
        // console.log(valid)
        // console.log(this.loginForm)  //绑定的是表单里面的值
        if (!valid) return console.log('erroe')
        const { data: res } = await this.$http.post('login', this.loginForm)
        // console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error('登录失败：' + res.meta.msg)
        }
        this.$message.success('登录成功')
        // 只有在登录之后才能访问，token只能在网站你打开的时候才能生效，所以将token保存到sessionStorage中然后才可以跳转到后台首页
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  position: absolute;
  width: 450px;
  height: 300px;
  background-color: #fff;
  transform: translate(-50%, -50%);
  left: 50%;
  top: 50%;
  border-radius: 3px;

  .avatar_box {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 130px;
    height: 130px;
    background-color: #fff;
    border-radius: 50%;
    padding: 10px;
    border: 1px solid #eee;
    box-shadow: 0 0 10px #ddd;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .form_box {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    box-sizing: border-box;
    padding: 0 20px;
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
